<template>
  <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="按类型配置" name="first">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="queryParams.name" @input="handleQuery" placeholder="请输入分类名称" clearable
              @keyup.enter="handleQuery" class="!w-240px" />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" class="mr-5px" /> 搜索
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table border row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
          @selection-change="handleRowCheckboxChange">
          <el-table-column label="分类名称" align="center" prop="name" />
          <el-table-column label="保养类型" align="center" prop="keepConfigName" />
          <el-table-column label="描述" align="center" prop="description">
            <template #default="scope">
              <div class="rich-text-content" v-html="scope.row.description"></div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150px">
            <template #default="scope">
              <el-button link type="primary" @click="openForm(1, scope.row.id)"
                v-hasPermi="['business:equipment-keep-config:config']">
                保养配置
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
          @pagination="getList" />
      </ContentWrap>
    </el-tab-pane>

    <el-tab-pane label="按设备配置" name="second">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form class="-mb-15px" :model="equipmentQueryParams" :inline="true" label-width="68px">
          <el-form-item label="设备名称" prop="equipmentName">
            <el-input v-model="equipmentQueryParams.equipmentName" placeholder="请输入设备名称" clearable @input="handleQuery"
              @keyup.enter="handleQuery" class="!w-240px" />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" class="mr-5px" /> 搜索
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>
      <ContentWrap>
        <el-table row-key="id" border v-loading="loading" :data="equipmentList" :stripe="true"
          :show-overflow-tooltip="true" @selection-change="handleRowCheckboxChange">
          <el-table-column label="设备名称" align="center" prop="equipmentName" width="120px">
            <template #default="{ row }">
              <el-button type="success" text style="font-size: 14px;" @click="openViewDialog(row.id)">
                {{ row.equipmentName }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="设备编码" align="center" prop="code" width="120px" />
          <el-table-column label="所属部门" align="center" prop="deptName" width="120px" />
          <el-table-column label="设备分类" align="center" prop="equipmentClassifyName" width="120px" />
          <el-table-column label="保养类型" align="center" prop="keepConfigName" />
          <el-table-column label="规格型号" align="center" prop="equipmentModel" width="120px" />
          <el-table-column label="操作" align="center" width="150px">
            <template #default="scope">
              <el-button link type="primary" @click="openForm(2, scope.row.id)"
                v-hasPermi="['business:equipment-keep-config:config']">
                保养配置
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination :total="equipmentTotal" v-model:page="equipmentQueryParams.pageNo"
          v-model:limit="equipmentQueryParams.pageSize" @pagination="getEquipmentList" />
      </ContentWrap>
    </el-tab-pane>
  </el-tabs>

  <!-- 表单弹窗：添加/修改 -->
  <EquipmentKeepConfigForm ref="formRef" @success="handleQuery" />

  <!-- 详细信息 -->
  <EquipmentInfoView ref="viewRef" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { isEmpty } from '@/utils/is'
import download from '@/utils/download'
import EquipmentKeepConfigForm from './EquipmentKeepConfigForm.vue'
import EquipmentInfoView from '../equipmentmanage/EquipmentInfoView.vue'
import { EquipmentManageApi, EquipmentManage } from '@/api/business/equipmentmanage'
import { EquipmentTypeApi, EquipmentType } from '@/api/business/equipmenttype'

/** 设备保养分类 列表 */
defineOptions({ name: 'EquipmentKeepType' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const equipmentList = ref([]) // 设备列表的数据
const total = ref(0) // 列表的总页数
const equipmentTotal = ref(0) // 设备列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  status: undefined,
  description: undefined
})

const equipmentQueryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  equipmentName: undefined,
  deptName: undefined,
  equipmentClassify: undefined,
  equipmentStatus: undefined,
  equipmentType: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const activeName = ref('first')
async function handleClick(item, event) {
  // activeName
  console.log("tabs", item.paneName, event)
  if (item.paneName == 'second') {
    await getEquipmentList()
  }
  activeName.value = item.paneName


}

//查看设备详细
const viewRef = ref()

const openViewDialog = (id) => {
  viewRef.value.openDetailInfo(id)
}

/** 查询列表 */
const getEquipmentList = async () => {
  try {
    const data = await EquipmentManageApi.getEquipmentManagePage(equipmentQueryParams)
    equipmentList.value = data.list
    equipmentTotal.value = data.total
  } finally {
  }
}


/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EquipmentTypeApi.getEquipmentTypePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList();
  equipmentQueryParams.pageNo = 1
  getEquipmentList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()

//


const openForm = (type: number, id?: number) => {
  formRef.value.open(type, id)
}



const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: EquipmentKeepType[]) => {
  checkedIds.value = records.map((item) => item.id);
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
